#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.8.1
 Author:         myName

 Script Function:
	Template AutoIt script.

#ce ----------------------------------------------------------------------------

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>


Global $test_points[15][2] = [ _
[18,30],[65,69],[130,40],[118,99],[186,133], _
[246,121],[293,79],[282,109],[320,76],[326,107], _
[399,96],[421,29],[464,8],[458,55],[488,6] _
]
Global $test_result[15][40][4]
For $i = 0 To 14
   $test_result[$i][0][0] = 0
Next
Global $file_out = "colors-out.txt"
Global $file_in = "colors-in.txt"


Func count_test(ByRef $tBits, $width)
   Local $cnt[2] = [0, 0]
   For $i = 0 To 14
	  Local $shift = $test_points[$i][0] + $test_points[$i][1] * $width + 1
	  Local $color = BitAND(DllStructGetData($tBits, 1, $shift),0xffffff)
	  If $color = 0 Then
		 _DebugOut($test_points[$i][0] & "," &  $test_points[$i][1] & " wrong color " & Hex(DllStructGetData($tBits, 1, $shift)))
	  EndIf
	  For $j = 1 To $test_result[$i][0][0]
		 If $test_result[$i][$j][0] = $color Then
			$cnt[$test_result[$i][$j][3] - 1] += $test_result[$i][$j][2]
		 EndIf
	  Next
   Next
   Return $cnt
EndFunc


Func reg_test(ByRef $tBits, $width, $nimg)
   Local $tmp[15][3]
   For $i = 0 To 14
	  Local $shift = $test_points[$i][0] + $test_points[$i][1] * $width + 1
	  Local $color = BitAND(DllStructGetData($tBits, 1, $shift),0xffffff)
	  If $color = 0 Then
		 _DebugOut($test_points[$i][0] & "," &  $test_points[$i][1] & " wrong color " & Hex(DllStructGetData($tBits, 1, $shift)))
	  EndIf
	  Local $new = True
	  For $j = 1 To $test_result[$i][0][0]
		 If $test_result[$i][$j][0] = $color Then
			$test_result[$i][$j][1] &= ", " & $nimg
			$new = False
			$tmp[$i][0] = $test_result[$i][$j][2]
			$tmp[$i][1] = $test_result[$i][$j][3]
			$tmp[$i][2] = $color
			ExitLoop
		 EndIf
	  Next
	  If $new And $test_result[$i][0][0] < 39 Then
		 $test_result[$i][0][0] += 1
		 Local $j = $test_result[$i][0][0]
		 $test_result[$i][$j][0] = $color
		 $test_result[$i][$j][1] = $nimg
		 $test_result[$i][$j][2] = 0
		 $test_result[$i][$j][3] = 0
		 $tmp[$i][0] = 0
		 $tmp[$i][1] = 0
		 $tmp[$i][2] = $color
	  EndIf
   Next
   open_window($tmp)
EndFunc

Func store_test($filename)
   Local $fh = FileOpen($filename, BitOR(2,256))
   For $i = 0 To 14
	  FileWrite($fh, "[" & $test_points[$i][0] & "," & $test_points[$i][1] & "] " & $test_result[$i][0][0] & @CRLF)
	  For $t = 2 To 1 Step -1
		 For $n = 3 To 1 Step -1
			Local $found = 0
			For $j = 1 To $test_result[$i][0][0]
			   If $test_result[$i][$j][2] = $n And $test_result[$i][$j][3] = $t Then 
				  FileWrite($fh, "0x" & Hex($test_result[$i][$j][0],6) & " : " &  _
					 $test_result[$i][$j][2] & "-" & $test_result[$i][$j][3] & _
					 " : " &  $test_result[$i][$j][1] & @CRLF)
				  $found += 1
			   EndIf
			Next
			If $found = 0 And $n <> 0 And $t <> 0 Then
			   _DebugOut("point " & $i & " test for " & $n & "-" & $t & " not found")
			;Else
			;   _DebugOut($n & "-" & $t & " found: " & $found)
			EndIf
		 Next
	  Next
   Next
   Fileclose($fh)
EndFunc

Func find_in_test($x,$y)
   For $i = 0 To 14
	  If $test_points[$i][0] = $x And $test_points[$i][1] = $y Then Return $i
   Next
   Return -1
EndFunc

Func load_test($filename)
   Local $fh = FileOpen($filename)
   Local $current = -1
   While True
	  Local $line = FileReadLine($fh)
	  If @error = -1 Then ExitLoop
	  Local $ret = StringRegExp($line,"\[(\d+),(\d+)\] (\d+)",1)
	  If @error = 0 Then
		 Local $i = find_in_test($ret[0],$ret[1])
		 If $i < 0 Then 
			_DebugOut("bad string '" & $line & "'")
			Exit
		 EndIf
		 $current = $i
		 $test_result[$i][0][0] = 0
	  ElseIf @error = 2 Then
		 _DebugOut("bad regexp against line '" & $line & "'")
		 Exit
	  Else
		 Local $ret = StringRegExp($line,"(0x[\da-fA-F]+) : (\d)-(\d)",1)
		 If @error = 0 Then
			Local $i = $current
			$test_result[$i][0][0] += 1
			Local $j = $test_result[$i][0][0]
			$test_result[$i][$j][0] = $ret[0]
			$test_result[$i][$j][1] = ""
			$test_result[$i][$j][2] = $ret[1]
			$test_result[$i][$j][3] = $ret[2]
		 ElseIf @error = 2 Then
			_DebugOut("bad regexp against line2 '" & $line & "'")
			Exit
		 Else
			_DebugOut("bad line '" & $line & "'")
		 EndIf
	  EndIf
   WEnd
   Fileclose($fh)
EndFunc

Global $swin = -1
Global $btn_ok
Global $radio[15][8]

Func prepare_win()
   load_test($file_in)

   $swin = GUICreate("advice",340,400,1200,50,BitOR($WS_CAPTION, $WS_POPUP),-1,$hGUI)
   For $i = 0 To 14
	  GUICtrlCreateLabel($i+1,0, 15 * $i, 30, 15)
	  $radio[$i][0] = GUICtrlCreateInput("", 30, 15 * $i, 60, 15)
	  GUIStartGroup()
	  $radio[$i][1] = GUICtrlCreateRadio(0, 90 + 30, 15 * $i, 30, 15)
	  $radio[$i][2] = GUICtrlCreateRadio(1, 90 + 60, 15 * $i, 30, 15)
	  $radio[$i][3] = GUICtrlCreateRadio(2, 90 + 90, 15 * $i, 30, 15)
	  $radio[$i][4] = GUICtrlCreateRadio(3, 90 + 120, 15 * $i, 30, 15)
	  GUICtrlSetState($radio[$i][1], $GUI_CHECKED)
	  GUIStartGroup()
	  $radio[$i][5] = GUICtrlCreateRadio("n", 100 + 150, 15 * $i, 30, 15)
	  $radio[$i][6] = GUICtrlCreateRadio("e", 100 + 180, 15 * $i, 30, 15)
	  $radio[$i][7] = GUICtrlCreateRadio("c", 100 + 210, 15 * $i, 30, 15)
	  GUICtrlSetState($radio[$i][5], $GUI_CHECKED)
   Next
   $btn_ok = GUICtrlCreateButton("Save", 0, 250, 60, 30)
EndFunc

Func open_window(ByRef $tmp)
   If $swin = -1 Then
	  _DebugOut("not prepared")
	  Exit
   EndIf
   For $i = 0 To 14
	  GUICtrlSetState($radio[$i][1 + $tmp[$i][0]], $GUI_CHECKED)
	  GUICtrlSetState($radio[$i][5 + $tmp[$i][1]], $GUI_CHECKED)
	  GUICtrlSetData($radio[$i][0], "0x" & Hex($tmp[$i][2],6))
   Next
   Local $pos = WinGetPos($hGUI)
   GUISetState(@SW_DISABLE, $hGUI) 
   GUISetState(@SW_SHOW, $swin)
   WinMove($swin,"",$pos[0] + 10,$pos[1] + 10)
EndFunc

Func StoreTestResult()
   For $i = 0 To 14
	  Local $n = 0, $t = 0
	  For $j = 2 To 4
		 If	BitAND($GUI_CHECKED, GUICtrlRead($radio[$i][$j])) Then
			$n = $j - 1
			ExitLoop
		 EndIf
	  Next
	  For $j = 6 To 7
		 If	BitAND($GUI_CHECKED, GUICtrlRead($radio[$i][$j])) Then
			$t = $j - 5
			ExitLoop
		 EndIf
	  Next
	  Local $color = GUICtrlRead($radio[$i][0])
	  ;_DebugOut("n=" & $n & " t=" & $t & " c=" & Hex($color,6) & " " & $color)
	  If $t > 0 And $n > 0 Then
		 Local $color = GUICtrlRead($radio[$i][0])
		 For $j = 1 To $test_result[$i][0][0]
			If $test_result[$i][$j][0] = $color Then
			   If _
			   $test_result[$i][$j][2] <> $n Or _
			   $test_result[$i][$j][3] <> $t Then
				  _DebugOut("overwrite for " & $i & " color=0x" & Hex($color,6) & ", was " & $test_result[$i][$j][2] & "-" & $test_result[$i][$j][3] & ", now " & $n & "-" & $t)
			   EndIf
			   $test_result[$i][$j][2] = $n
			   $test_result[$i][$j][3] = $t
			   ExitLoop
			EndIf
		 Next
	  EndIf
   Next

   GUISetState(@SW_ENABLE, $hGUI) 
   GUISetState(@SW_HIDE, $swin)
   store_test($file_out)
EndFunc

